package com.qf.controller;

import com.qf.util.DownLoadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/*
杨威
2021/9/17
14:49
*/
@WebServlet("/fileList")
public class FileListController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将所有图片都取出来，存在map集合中
        //1先获取保存文件的目录
        String realPath = request.getServletContext().getRealPath("/WEB-INF/upload");
        //2创建一个map结合
        Map<String,String> map=new HashMap();
        //3调用工具类存储key和value
        DownLoadUtil.listFiles(new File(realPath),map);
        //存在作用域
        request.setAttribute("listFiles",map);
        //跳转到下载页面
        request.getRequestDispatcher("/fileList.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
